﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Windows.Forms;
using System.Threading;

namespace weiqijiance.信号处理模块
{
    public class DACnew : DACBase
    {

        /// <summary>
        /// 打开串口
        /// </summary>
        /// <param name="sSet1">串口设置</param>
        /// <returns>是否成功打开</returns>
        public override bool openCOM(string sSet1)
        {
            buf = new byte[10000];
            commex = new SerialPort();
            bool breturn = base.openCOM(sSet1);
            commex.ReadTimeout = 500;
            //lzj commex.ReceivedBytesThreshold = 52;//达到50字节以后才开始触发串口
            commex.ReceivedBytesThreshold = 26;//达到50字节以后才开始触发串口
            commex.DataReceived += comDAC_DataReceived;
            return breturn;
        }
        private List<byte> buffer = new List<byte>(4096);
        public int iDAcount = 0;
        public DateTime comDA100;

        public bool bread;
        public byte[] buf;
        List<byte> xhblist = new List<byte>();
        void comDAC_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (commex.IsOpen && !bread)
            {
                bread = true;

                int n = commex.BytesToRead;//
                if (iDAcount == 0)
                {
                    iDAcount = n;
                }
                else if ((iDAcount + n) > 10000)
                {
                    buf = new byte[10000];
                    iDAcount = n;
                }
                else
                {
                    iDAcount = iDAcount + n;
                }

                byte[] buf1 = new byte[n];//声明一个临时数组存储当前来的串口数据  
                commex.Read(buf1, 0, n);
                commex.DiscardInBuffer();

                var data = "";
                for (int i = 0; i < n; i++)
                {
                    data = data + string.Format("{0:X} ", buf1[i]);
                }


                //   Log.WriteLog(string.Format("返回内容：chang:{0} data:{1}", n,data));


                if (n > 25)
                {
                    for (int j = 0; j < n; j++)
                    {
                        buf[iDAcount - n + j] = buf1[j];
                    }
                    for (int kk = iDAcount - 26; kk > 0; kk--)
                    {
                        if (buf[kk] == 0xFF && buf[kk + 23] == 0xFE)
                        {
                            short uSum1 = 0;
                            for (int i = 0; i < 24; i++)
                            {
                                uSum1 += buf[kk + i];
                            }

                            short uSum = 0;
                            uSum = (short)(buf[kk + 24] * 256 + buf[kk + 25]);
                            if (uSum1 == uSum)
                            {
                                //Log.WriteLog(string.Format("解析通过：新接收到长度n:{0} 总点数:{1}；解析通过点：{2}", n, iDAcount, kk));
                                //模拟
                                for (int i = 0; i < 5; i++)//模拟4路
                                {
                                    int fAIDataxh = buf[kk + i * 4 + 2] * 256 * 256 + buf[kk + i * 4 + 3] * 256 + buf[kk + i * 4 + 4];
                                    if (fAIDataxh > 0x03FFFF)
                                    {
                                        fAIDataxh = 0 - (fAIDataxh ^ 0x7FFFF);
                                        fAIDataex[i] = fAIDataxh + (262143);//
                                    }
                                    else
                                    {
                                        fAIDataex[i] = buf[kk + i * 4 + 2] * 256 * 256 + buf[kk + i * 4 + 3] * 256 + buf[kk + i * 4 + 4] + 262143;
                                    }
                                }
                                ///////速度
                                for (int m = 0; m < 2; m++)// 
                                {

                                    fTIData[m] = buf[kk + 19 + 2 * m] * 256 + buf[kk + 20 + 2 * m];

                                }
                                ////////红外
                                //66  18

                                for (int k = 0; k < 1; k++)//
                                {
                                    for (int i = 0; i < 8; i++)//红外
                                    {
                                        int ihongwai = 0;
                                        //  ihongwai = buf[kk + k * 2 + 18] >> i;
                                        ihongwai = ((buf[kk + k * 2 + 18] & (int)Math.Pow(2, i)) == (int)Math.Pow(2, i)) ? 1 : 0;

                                        if (ihongwai == 1)
                                        {
                                            bDIData[i] = true;
                                        }
                                        else
                                        {
                                            bDIData[i] = false;
                                        }
                                    }


                                }
                                ////////红外


                                // xhblist.Clear();
                                break;
                            }
                        }
                        else
                        {
                            // Log.WriteLog(string.Format("解析过程：icount:{0}；kk：{1}",  iDAcount, kk));
                        }
                    }
                }

                bread = false;

            }
        }

        public double DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            double dateDiff = 0;
            TimeSpan ts = DateTime1.Subtract(DateTime2).Duration();
            dateDiff = ts.Days * 60 * 60 * 60 * 1000 + ts.Hours * 60 * 60 * 1000 + ts.Minutes * 60 * 1000 + ts.Seconds * 1000 + ts.Milliseconds;
            return dateDiff;
        }
        //----------------------------------------------------------------------
    }
}

